package com.ruoyi.system.service.impl.trade;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.db.basic.BasicMaterials;
import com.ruoyi.system.domain.dto.MaterialOrderDto;
import com.ruoyi.system.domain.vo.MaterialOrderVo;
import com.ruoyi.system.mapper.basic.BasicMaterialsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.trade.TradeMaterialOrderMapper;
import com.ruoyi.system.domain.db.trade.TradeMaterialOrder;
import com.ruoyi.system.service.ITradeMaterialOrderService;

/**
 * 物资交易管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
@Slf4j
public class TradeMaterialOrderServiceImpl implements ITradeMaterialOrderService 
{
    @Autowired
    private TradeMaterialOrderMapper tradeMaterialOrderMapper;

    @Autowired
    private BasicMaterialsMapper basicMaterialsMapper;

    /**
     * 查询物资交易管理
     * 
     * @param orderId 物资交易管理主键
     * @return 物资交易管理
     */
    @Override
    public MaterialOrderVo selectTradeMaterialOrderByOrderId(Long orderId)
    {
        MaterialOrderVo materialOrderVo = new MaterialOrderVo();
        if(orderId != null){
            MaterialOrderDto materialOrderDto = tradeMaterialOrderMapper.selectTradeMaterialOrderByOrderId(orderId);
            materialOrderVo = dtoToVo(materialOrderDto);
        }
        List<BasicMaterials> basicMaterials = basicMaterialsMapper.selectAllBasicMaterials();
        materialOrderVo.setMaterialList(basicMaterials);
        return materialOrderVo;
    }

    /**
     * 查询物资交易管理列表
     * 
     * @param tradeMaterialOrder 物资交易管理
     * @return 物资交易管理
     */
    @Override
    public List<MaterialOrderVo> selectTradeMaterialOrderList(TradeMaterialOrder tradeMaterialOrder)
    {

        List<MaterialOrderDto> materialOrderDtos = tradeMaterialOrderMapper.selectTradeMaterialOrderList(tradeMaterialOrder);
        return materialOrderDtos.stream().map(this::dtoToVo).collect(Collectors.toList());
    }

    /**
     * 新增物资交易管理
     * 
     * @param tradeMaterialOrder 物资交易管理
     * @return 结果
     */
    @Override
    public int insertTradeMaterialOrder(TradeMaterialOrder tradeMaterialOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        tradeMaterialOrder.setCreateTime(nowDate);
        tradeMaterialOrder.setCreateBy(loginUser.getUser().getUserId());
        tradeMaterialOrder.setUpdateBy(loginUser.getUser().getUserId());
        tradeMaterialOrder.setUpdateTime(nowDate);
        tradeMaterialOrder.setTotal(tradeMaterialOrder.getPrice() * tradeMaterialOrder.getQuantity());
        return tradeMaterialOrderMapper.insertTradeMaterialOrder(tradeMaterialOrder);
    }

    /**
     * 修改物资交易管理
     * 
     * @param tradeMaterialOrder 物资交易管理
     * @return 结果
     */
    @Override
    public int updateTradeMaterialOrder(TradeMaterialOrder tradeMaterialOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        tradeMaterialOrder.setUpdateBy(loginUser.getUser().getUserId());
        tradeMaterialOrder.setUpdateTime(nowDate);
        tradeMaterialOrder.setTotal(tradeMaterialOrder.getPrice() * tradeMaterialOrder.getQuantity());
        return tradeMaterialOrderMapper.updateTradeMaterialOrder(tradeMaterialOrder);
    }

    /**
     * 批量删除物资交易管理
     * 
     * @param orderIds 需要删除的物资交易管理主键
     * @return 结果
     */
    @Override
    public int deleteTradeMaterialOrderByOrderIds(Long[] orderIds)
    {
        return tradeMaterialOrderMapper.deleteTradeMaterialOrderByOrderIds(orderIds);
    }

    /**
     * 删除物资交易管理信息
     * 
     * @param orderId 物资交易管理主键
     * @return 结果
     */
    @Override
    public int deleteTradeMaterialOrderByOrderId(Long orderId)
    {
        return tradeMaterialOrderMapper.deleteTradeMaterialOrderByOrderId(orderId);
    }

    public MaterialOrderVo dtoToVo(MaterialOrderDto dto) {
        MaterialOrderVo vo = new MaterialOrderVo();
        vo.setMaterialId(dto.getMaterial().getMaterialId());
        vo.setOrderId(dto.getOrderId());
        vo.setMaterialTypeName(dto.getMaterial().getMaterialTypeName());
        vo.setApplicationType(dto.getMaterial().getApplicationType());
        vo.setMaterialName(dto.getMaterial().getMaterialName());
        vo.setQuantity(dto.getQuantity());
        vo.setPrice(dto.getPrice());
        vo.setTotal(dto.getTotal());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        return vo;
    }
}
